package com.ehlxrStudy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * 按比例控制流量
 */
public class RateBarrier {
    private final AtomicInteger op = new AtomicInteger(0);
    private List<Integer> source;
    private int base;

    public int rate() {
        return source.get(op.incrementAndGet() % base);
    }

    private RateBarrier() {
    }

    public RateBarrier(int base) {
        this.base = base;

        source = new ArrayList<>(base);
        for (int i = 0; i < base; i++) {
            source.add(i);
        }

        // 打乱集合顺序
        Collections.shuffle(source);
    }

    public static void main(String[] args) {
        RateBarrier rateBarrier = new RateBarrier(10);

        IntStream.range(0, 20).parallel().forEach(i -> {
            int rate = rateBarrier.rate();
//            if (rate < 2) {
//                System.out.println("this is on 2");
//            } else if (rate < 5) {
//                System.out.println("this is on 3");
//            } else {
//                System.out.println("this is on 5");
//            }
            System.out.println(rate);
        });

        // final Thread[] threads = new Thread[20];
        // for (int i = 0; i < threads.length; i++) {
        //     threads[i] = new Thread(() -> {
        //         if (rateBarrier.allow()) {
        //             System.out.println("this is on 3");
        //         } else {
        //             System.out.println("this is on 7");
        //         }
        //     });
        //     threads[i].start();
        // }
        //
        // for (Thread t : threads) {
        //     try {
        //         t.join();
        //     } catch (InterruptedException e) {
        //         e.printStackTrace();
        //     }
        // }

    }
}

// Output:

/*
this is on 3
this is on 3
this is on 5
this is on 2
this is on 5
this is on 5
this is on 5
this is on 3
this is on 3
this is on 3
this is on 5
this is on 2
this is on 2
this is on 5
this is on 5
this is on 5
this is on 5
this is on 3
this is on 2
this is on 5
*/
